'use strict';
const { Controller } = require('egg');
const { Op } = require('sequelize');
const dayjs = require("dayjs");
const serviceName = 'wx'
function isEmpty (val) {
    return val === null || val === undefined || val === '' || val.length === 0 || JSON.stringify(val) === '{}' || JSON.stringify(val) === '[]'|| val === 'null' || val === 'undefined'
}
function getStoreQueryParams(query) {
    const { company_id, type, date } = query;
    let where = {
        company: company_id,
    }
    let key = ''
    if (type === '1') {
        key= 'buytime'
    } else if (type === '0') {
        key = 'outtime'
    }
    if (date) {
        let splitDate = date.split('-')
        if (splitDate.length === 3) {
            // 日期
            where[key] = {
                // 这个月或者这天
                [Op.between]: [`${date} 00:00:00`, `${date} 23:59:59`]
            }
        } else if (splitDate.length === 2) {
            // 月份
            where[key] = {
                // 这个月
                [Op.between]: [`${date}-01 00:00:00`, `${date}-31 23:59:59`]
            }
        }

    }
    return where
}
class RouterController extends Controller {
    async bannerList() {
        const { service, query } = this.ctx;
        const list = await service[serviceName].bannerList();
        this.ctx.success(list);
    }
    async bannerDetail() {
        const { service, query } = this.ctx;
        const { id } = query;
        if (!id) {
            this.ctx.error('id不能为空');
            return;
        }
        const detail = await service[serviceName].bannerDetail(id);
        this.ctx.success(detail);
    }
    async companyList() {
        const { service, query } = this.ctx;
        const list = await service[serviceName].companyList();
        this.ctx.success(list);
    }
    // 仓库记录
    async storeRecordList() {
        const { service, query } = this.ctx;
        if (isEmpty(query.company_id)) {
            this.ctx.error(500, '牛场不能为空');
            return
        }
        if (isEmpty(query.type)) {
            this.ctx.error(500, '出入库类型不能为空');
            return
        }
        const where = getStoreQueryParams(query);
        const list = await service[serviceName].storeRecordList(where, query.type);
        this.ctx.success(list);
    }
    // 饲料种类
    async matList() {
        const { service,query } = this.ctx;
        const { date } = query
        let where = {
            is_deleted: 0,  // 未删除
            outtime: {
                // 当月第一天至最后一天
                [Op.between]: [`${dayjs(date).startOf('month').format('YYYY-MM-DD')} 00:00:00`, `${dayjs(date).endOf('month').format('YYYY-MM-DD')} 23:59:59`]
            }
        }
        let list = await service[serviceName].matList(where);
        this.ctx.success(list);
    }
    // 饲料前六个月平均价格
    async matinAvgPrice() {
        const { service, query } = this.ctx;
        if (isEmpty(query.company_id)) {
            this.ctx.error(500, '牛场不能为空');
            return
        }
        if (isEmpty(query.mat)) {
            this.ctx.error(500, '物料不能为空');
            return
        }
        const list = await service[serviceName].matinAvgPrice(query);
        this.ctx.success(list)
    }

    // 牛舍
    async roomList() {
        const { service, query } = this.ctx;
        if (isEmpty(query.company_id)) {
            this.ctx.error(500, '牛场不能为空');
            return
        }
        const list = await service[serviceName].roomList(query);
        this.ctx.success(list);
    }
    // 牛舍设备
    async deviceList() {
        const { service, query } = this.ctx;
        const list = await service[serviceName].deviceList(query);
        this.ctx.success(list);
    }

    // 设备故障记录
    async deviceFaultsList() {
        const { service, query } = this.ctx;
        if (isEmpty(query.date)) {
            this.ctx.error(500, '日期不能为空');
            return
        }
        const list = await service[serviceName].deviceFaultsList(query);
        this.ctx.success(list)
    }
    async cameraList() {
        const { service, query } = this.ctx;
        const list = await service[serviceName].cameraList(query);
        this.ctx.success(list)
    }

    /**
     * 牛相关
     */
    async beefGroupList() {
        const { service, query } = this.ctx;
        let { company_id } = query;
        if (isEmpty(company_id)) {
            this.ctx.error(500, '牛场不能为空');
            return
        }
        const list = await service[serviceName].beefGroupList({
            company: company_id,
        });
        this.ctx.success(list)
    }
    async beefInOutList() {
        const { service, query } = this.ctx;
        let { company_id } = query;
        if (isEmpty(company_id)) {
            this.ctx.error(500, '牛场不能为空');
            return
        }
        let where = {
            status: '已接收'
        }
        // nowcomp 或者 tocomp
        where[Op.or] = [
            { nowcomp: company_id },
            { tocomp: company_id }
        ]

        const list = await service[serviceName].beefInOutList(where);
        this.ctx.success(list)
    }

    /**
     * 告警清单
     */
    async alarmList() {
        const { service, query } = this.ctx;
        const list = await service[serviceName].alarmList(query);
        this.ctx.success(list)
    }
    async handleAlarm() {
        const { service, request } = this.ctx;
        const { body } = request;
        if (!body.id) {
            this.ctx.error(500, 'id不能为空');
            return
        }
        const result = await service[serviceName].handleAlarm(body);
        this.ctx.success(result)
    }
    async deleteAlarm() {
        const { service, request } = this.ctx;
        const { body } = request;
        if (!body.id) {
            this.ctx.error(500, 'id不能为空');
            return
        }
        const result = await service[serviceName].deleteAlarm(body);
        this.ctx.success(result)
    }

    /**
     * 考勤相关
     */
    async workLogToday() {
        const { service, query } = this.ctx;
        const { company_id } = query;
        if (!company_id) {
            this.ctx.error(500, '牛场不能为空');
            return
        }
        const list = await service[serviceName].workLogToday(company_id);
        this.ctx.success(list)
    }

    /**
     * 年龄分布
     */
    async ageDistribution() {
        const { service, query } = this.ctx;
        const { company_id } = query;
        if (!company_id) {
            this.ctx.error(500, '牛场不能为空');
            return
        }
        const list = await service[serviceName].ageDistribution(company_id);
        this.ctx.success(list)
    }

    /**
     * 生产计划相关
     */
    async planList() {
        const { service, query } = this.ctx;
        const { company_id } = query;
        if (!company_id) {
            this.ctx.error(500, '牛场不能为空');
            return
        }
        const list = await service[serviceName].planList(company_id);
        this.ctx.success(list)
    }
    /**
     * 登录
     */
    async login() {
        const { service, request } = this.ctx;
        const { body } = request;
        const result = await service[serviceName].login(body);
        this.ctx.success(result)
    }

    /**
     * 繁育指标
     */
    async breedIndex() {
        const { service, query } = this.ctx;
        const { company_id } = query;
        if (!company_id) {
            this.ctx.error(500, '牛场不能为空');
            return
        }
        const list = await service[serviceName].breedIndex(query);
        this.ctx.success(list)
    }

    /**
     * 疾病
     */
    async diseaseList() {
        const { service, query } = this.ctx;
        const { company_id } = query;
        if (!company_id) {
            this.ctx.error(500, '牛场不能为空');
            return
        }
        const list = await service[serviceName].diseaseList(query);
        this.ctx.success(list)
    }
}

module.exports = RouterController;
